package org.gx.tool.hptool.database.helper;

import java.util.Set;

import org.gx.tool.hptool.ToolBeanEntity;
import org.gx.tool.hptool.convert.FieldTypeConvertScheme;
import org.gx.tool.hptool.helper.AbstractToolHelper;
import org.gx.tool.hptool.pojo.EntitySturct;
import org.gx.tool.hptool.pojo.FieldPorperty;
import org.gx.tool.hptool.pojo.Project;
import org.gx.tool.hptool.processer.DataProcesser;
import org.gx.tool.hptool.processer.XLSFileDataProcesser;

public class DataBaseFileToolHelper < T extends ToolBeanEntity> extends AbstractToolHelper <T> {

	private DataProcesser reader ;
	private FieldTypeConvertScheme convertSchemem;
	private final static String CREATE_TABLE = "create table ";
	private final static String  SPACE_CHARACTER = " ";
	private final static String LEFT_FOUR_SPACE = "    ";
	private final static String LEFT_PARENTHESES = " ( ";
	private final static String RIGHT_PARENTHESES = " ) ";
	private final static String PRIMARY_KEY = " primary key ";
	private final static String CHANG_LINE = "\n";
	private final static String COMMA = ",";
	private final static String SEMICOLON = ";";
	/**
	 * 1、输出文本
	 */
	
	public DataBaseFileToolHelper(DataProcesser reader) {
		this.reader = reader;
	}
	
	@Override
	public String outText(Project project,FieldTypeConvertScheme convertScheme ) {
		StringBuffer sqlText = new StringBuffer();
		if (null != project) {
			Set<EntitySturct> sturcts = project.getSturcts();
			if (null != sturcts && !sturcts.isEmpty()) {
				StringBuffer tableSql = new StringBuffer();
				for(EntitySturct sturct : sturcts) {
					tableSql.append(CREATE_TABLE).append(sturct.getSturctName()) ;
					tableSql.append(LEFT_PARENTHESES).append(CHANG_LINE);
					Set<FieldPorperty> fields = sturct.getFields();
					int count = fields.size();
					for (FieldPorperty field :  fields) {
						count--;
						tableSql.append(LEFT_FOUR_SPACE);
						String convertType = convertScheme.getConertType(field.getFieldType());
						tableSql.append(field.getField()).append(SPACE_CHARACTER).append(convertType);
						int intLen = field.getIntegerLength();
						if (intLen > 0) {
							tableSql.append(LEFT_PARENTHESES).append(intLen);
							int decimalsLen =  field.getDecimalsLength();
							if (decimalsLen > 0 ) {
								tableSql.append(COMMA).append(decimalsLen);
							}
							tableSql.append(RIGHT_PARENTHESES);
						}
						
						if (field.isPrimary()) {
							tableSql.append(PRIMARY_KEY);
						}
						if (count > 0) {
							tableSql.append(COMMA);
						}
						tableSql.append("--").append(field.getDescription());
						tableSql.append(CHANG_LINE);
						
					}
					
					tableSql.append(RIGHT_PARENTHESES).append(SEMICOLON).append(CHANG_LINE);
					
				}	sqlText.append(tableSql.toString());
			}
		}
		return sqlText.toString();
		
	} 

	@Override
	public Project getProjectData() {
		return reader.readProjectData();
		
	}

	
	
	@Override
	public EntitySturct getEntitySturctData() {
		
		return null;
	}

	

	public FieldTypeConvertScheme getConvertSchemem() {
		return convertSchemem;
	}

	public void setConvertSchemem(FieldTypeConvertScheme convertSchemem) {
		this.convertSchemem = convertSchemem;
	}

	
}